var $ = layui.jquery;
var layer = layui.layer;
var laytpl = layui.laytpl;
var laydate=layui.laydate;
var id = 1;
$("button").click(add);

function add() {
	layer.open({
		type: 1,
		content: $("#testa").html(),
		area: ["400px", "360px"],
		title: "添加信息",
		success: function() {
			$("input[value='确定']").click(testAdd);
			$("input[value='关闭']").click(testClose);
			renderDate("birth");
		}
	})
}

function testClose() {
	layer.close(layer.index)
}

function testAdd() {
	var name = $("input[name='name']").val();
	var age = $("input[name='age']").val();
	var birth=$("#birth").val();
	var data = {
		id: id,
		name: name,
		age: age,
		birth: birth
	}
	var html = $("#tradd").html();
	var temp = laytpl(html).render(data);
	$("#tbody").append(temp);
	id++;
	testClose();
}

function del(id) {

	layer.confirm("确认删除该记录？", function(index) {
		$("#tr" + id).remove();
		layer.close(index)
	})
}

function upd(id) {
	layer.open({
		type: 1,
		content: $("#testa").html(),
		area: ["400px", "360px"],
		title: "修改信息",
		success: function() {
			//姓名
			var name = $("#tdb" + id).text();
			var age = $("#tdc" + id).text();
			var birth=$("#tdd"+id).text();
			$("input[name='name']").val(name);
			$("input[name='age']").val(age);
			$("#birth").val(birth);
			renderDate("birth")
			$("input[value='确定']").click({id:id},testSave);
			$("input[value='关闭']").click(testClose);
		}
	})
}

function testSave(event){
	var id =event.data.id;
	$("#tdb"+id).html($("input[name='name']").val());
	$("#tdc"+id).html($("input[name='age']").val());
	$("#tdd"+id).html($("#birth").val())
	testClose();
}

function sel(){
	$("tbody tr").css("background","")
	var name2=$("input[name='name2']").val();
	if(name2=='')return;
	$("tbody tr").each(function(){
		if($(this).find("td[id^='tdb']").text().indexOf(name2)>-1)
			$(this).css("background","red")
	})
}

function renderDate(id){
	laydate.render({
		elem:"#"+id,
		type:"date"
	})
}
